Control Structures & Iterables — Code Cards (Classroom Version)

Try me

Open In ColabBinder

How to use

  • Each card below mirrors the A4 activity prompt. First try to complete the exercise by yourself, predict the output or find the bug before running.

  • Then run the code cell to verify, or run the Fix cell (when provided) to see a correct version.

  • Keep explanations short and schematic (what/why). ### Rendering Gemini into an interactive AI tutor

  • If you need help, hints, or extra exercises from AI, use the following prompt to convert it into an AI-tutor

You are a **coding tutor** for Python in Jupyter/Colab. Follow the **course motto** “do not give up learning.”

### Role & Goals
- Use **Socratic guidance** and **test-first thinking** to help me solve problems myself.
- Help me read errors, reason about state, and make small, safe iterations.

### Strict Rules
1) **Do not** provide full working solutions or paste complete functions/programs.
   - You may show **tiny illustrative fragments (≤3 lines)** or **pseudo-code with TODOs**, but not a drop-in answer.
2) Prefer **questions over answers**; offer **one small next step** at a time.
3) When debugging, explain **what the traceback says**, give **2–3 hypotheses**, and propose the **smallest diff** in *plain English* first.
4) Encourage **TDD**: ask me to write/assert a test, predict, run, and report outputs.
5) Keep responses concise (≈120–150 words) unless I ask for a deeper explanation or code review.
6) Ask me to **run code and share results**; adapt based on the output.
7) If I request the full solution, remind me of the rules and offer a **higher-tier hint** instead.
8) When I finalize an exercise, reinforce learning lessons and suggest additional exercises

### Interaction Loop (use this structure)
- **Restate goal:** what I’m trying to accomplish in one line.
- **Diagnose:** key assumption to check or error to interpret.
- **Hint (tiered):**
  - Tier 1: Conceptual nudge (no code).
  - Tier 2: Directed hint (identify line/construct to change).
  - Tier 3: Pseudo-code with TODOs or a **1–3 line** pattern (still not a full solution).
- **Next action:** one concrete step for me to try now.
- **Ask back:** what to run/paste (output, test result, or traceback).

### When reviewing my code
- Comment on **correctness, clarity, naming, and complexity (big-O)**.
- Suggest **tests** I’m missing (boundaries, empty cases, error paths).

### Safety & Ethics
- No secrets or private data in prompts.
- avoid library functions/APIs unless I ask.

Stay in tutor mode for the whole session.

Basic control structures - If-else statements

Card IF-1 — Predict the output

x = 20
if x < 20:
    msg = "A"
elif x <= 20:
    msg = "B"
else:
    msg = "C"
print(msg)

Question: What is printed?

[ ]:
# Card IF-1 — run to check
x = 20
if x < 20:
    msg = "A"
elif x <= 20:
    msg = "B"
else:
    msg = "C"
print(msg)  # expected: B

Card IF-2 — Predict the output

x = 19
msg = "X"
if x < 20:
    msg = "A"
if x < 19:
    msg = "B"
else:
    msg = "C"
print(msg)

Question: What is printed?

[ ]:
# Card IF-2 — run to check
x = 19
msg = "X"
if x < 20:
    msg = "A"
if x < 19:
    msg = "B"
else:
    msg = "C"
print(msg)  # expected: C (else binds to second if)

Card IF-3 — Predict the output

temp = 22
if temp > 20:
    if temp < 25:
        res = "A"
else:
    res = "B"
print(res)

Question: What is printed?

[ ]:
# Card IF-3 — run to check
temp = 22
if temp > 20:
    if temp < 25:
        res = "A"
else:
    res = "B"
print(res)  # expected: A (nested if is True; outer else is skipped entirely)

Card IF-4 — Code Wizard (Optimize the branching)

Original:

temp = 22
if temp > 20:
    if temp < 25:
        res = "A"
else:
    res = "B"
print(res)

Task: Rewrite with a clear if/elif/else chain.

[ ]:
# Card IF-4 — one possible optimization
temp = 22
if temp <= 20:
    res = "B"
elif temp < 25:
    res = "A"
else:
    res = "B"
print(res)  # clearer mutually-exclusive branches

Card IF-5 — Predict the output

score = 90
if score >= 80:
    grade = "A"
elif score >= 90:
    grade = "A+"
else:
    grade = "B"
print(grade)

Question: What is printed?

[ ]:
# Card IF-5 — run to check
score = 90
if score >= 80:
    grade = "A"
elif score >= 90:
    grade = "A+"
else:
    grade = "B"
print(grade)  # expected: A (first true branch wins)

2) while loops — Human Calculator & Code Detective

Card WH-1 — Code Detective (Find the bug)

Buggy code (indentation error):

i = 0
while i < 5:
  i += 1
    print(i)
  if i % 2 == 0:
    continue

Task: Fix indentation so it runs and behaves as intended.

[ ]:
# Card WH-1 — Fixed version
i = 0
while i < 5:
    i += 1
        print(i)
    if i % 2 == 0:
        continue

Card WH-2 — Predict the output

i = 3
while i > 0:
    print(i, end="")
    i -= 1
print("done")

Question: What is printed?

[ ]:
# Card WH-2 — run to check
i = 3
while i > 0:
    print(i, end="")
    i -= 1
print("done")  # expected: 321done

Card WH-3 — Human Calculator

n = 0
while True:
    n += 1
    if n == 3:
        continue
print(n)

Question: How many times does the loop run?

Card WH-4 — Predict the output

i, s = 0, 0
while i < 5:
  i += 1
  if i % 2 == 0:
    continue
  s += i
print(s)

Question: What is printed?

[ ]:
# Card WH-4 — run to check
i, s = 0, 0
while i < 5:
    i += 1
    if i % 2 == 0:
        continue
    s += i
print(s)

3) for loops — Human Calculator & Code Detective

Card FOR-1 — Predict the output

names = ["A","B"]
for i, n in enumerate(names, start=1):
    print(i, n)

Question: What is printed?

[ ]:
# Card FOR-1 — run to check
names = ["A","B"]
for i, n in enumerate(names, start=1):
    print(i, n)

Card FOR-2 — Predict the output

for x in [0,1,2]:
    if x:
        continue
    print(x)

Question: What is printed?

[ ]:
# Card FOR-2 — run to check
for x in [0,1,2]:
    if x:
        continue
    print(x)

Card FOR-3 — Predict the output

for i in range(5):
    print(2**i)

Question: What is printed?

[ ]:
# Card FOR-3 — run to check
for i in range(5):
    print(2**i)

Card FOR-4 — Code Detective (Find the bug)

Prompt comment:

# print odd numbers from 1 to 5 [1, 3, 5]
for i in range(5, 1, 1):
    print(i)

Task: Fix the loop to match the comment.

[ ]:
# Fix the loop to match the comment. Hint: check range arguments
for i in range(5, 1, 1):
    print(i)  # 1, 3, 5

Notes

  • if/elif/else: first true branch executes; else binds to the nearest unmatched if.

  • while True without break never terminates.

  • continue skips to the next iteration; break exits the loop.

  • enumerate(iterable, start=1) yields (index, item).

  • range(start, stop, step): stop is exclusive.

Extra exercises

Card EX-1 — Predict the output

for i in range(3):
    for j in range(2):
        if i == j:
            print(i, j)

Question: What is printed?

[ ]:
for i in range(3):
    for j in range(2):
        if i == j:
            print(i, j)

Card EX-2 — Code Wizard (Optimize the loop)

Task: Fix the code so that “Done” is printed when the loop completes, without unnecessary logic. Explain your changes.

i = 0
while i < 5:
    print(i)
    i += 1
    if i == 5:
        print("Done")
        break
[ ]:
i = 0
while i < 5:
    print(i)
    i += 1
    if i == 5:
        print("Done")
        break

Card EX-3 — Human Calculator (Predict the output)

What is printed?

s = 0
for i in range(1, 6, 2):
    if i % 2 == 0:
        s += i
    else:
        s += i * 2
print(s)
[ ]:
s = 0
for i in range(1, 6, 2):
    if i % 2 == 0:
        s += i
    else:
        s += i * 2
print(s)

Card EX-4 — Human Calculator (Predict the output)

What is printed?

a = [1, 2, 3, 4, 5, 6, 7]
print(a[::-2])
[ ]:
a = [1, 2, 3, 4, 5, 6, 7]
print(a[::-2])

Card EX-5 — Code Wizard (Fix slicing)

Task: Fix the code to print [“B”, “o”, “n”, “e”] using as much defaults as possible

b = ["B", "r", "o", "w", "n", "i", "e"]
print(b[1:7:2])
[5]:
b = ["B", "r", "o", "w", "n", "i", "e"]
print(b[1:7:2])
['r', 'w', 'i']